/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-07-12     
 */
package com.task.service.impl;

import com.task.bean.Constants;
import com.task.entity.Store;
import com.task.repository.IStoreRepository;
import com.task.service.IStoreService;
import com.task.utils.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 门店服务
 */
@Service
public class StoreServiceImpl implements IStoreService
{
    
    @Autowired
    IStoreRepository storeRepository;
    
    @Override
    public Page<Store> findStorePage(Map<String, Object> searchParams, int page, int rows)
    {
        PageRequest pageRequest = PageUtil.buildPageRequest(page, rows, Constants.DESC, Constants.ID);
        Specification<Store> spec = PageUtil.buildSpecification(searchParams, Store.class);
        return storeRepository.findAll(spec, pageRequest);
    }
    
    @Override
    public List<Store> findByCode(List<String> code)
    {
        return storeRepository.findByStoreCodes(code);
    }

    @Override
    public List<Store> findAll()
    {
        return storeRepository.findAll();
    }

    @Override
    public void saveOrUpdateStoreInfo(List<Store> storeList) {
        Store storeDb;
        for (Store store : storeList) {
            if (StringUtils.isNotBlank(store.getStoreCode())) {
                storeDb = storeRepository.findByStoreCode(store.getStoreCode());
                if (storeDb == null) {//新增
                    storeRepository.save(store);
                } else {//更新
                    if (StringUtils.isNotBlank(store.getStoreName())) {
                        storeDb.setStoreName(store.getStoreName());
                    }
                    if (StringUtils.isNotBlank(store.getStoreAddress())) {
                        storeDb.setStoreAddress(store.getStoreAddress());
                    }
                    storeRepository.save(storeDb);
                }
            }
        }
    }
}
